/**
 * logger.cpp is part of Gedule.
 *
 * Gedule is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gedule is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gedule.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "logger.h"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/PatternLayout.hh"

const std::string Logger::LOG_DIR = "log";

///////////////////////////////////////////////////////////////
Logger::Logger( const std::string & a_name, LoggerType a_type, Priority::PriorityLevel a_priority )
    : m_category ( log4cpp::Category::getInstance( a_name ) ),
      m_type( a_type )
{
    log4cpp::Appender * appender = GetAppender( a_name, a_type );
    log4cpp::PatternLayout * layout = new log4cpp::PatternLayout();
    layout->setConversionPattern( "%d{%d-%m-%Y %H:%M:%S,%l} - %c - %p - %m%n" );
    appender->setLayout( layout );
    m_category.setAppender( appender );
    m_category.setPriority( a_priority );
}

///////////////////////////////////////////////////////////////
CategoryStream Logger::LogDebug() const
{
    return m_category.debugStream();
}

///////////////////////////////////////////////////////////////
CategoryStream Logger::LogError() const
{
    return m_category.errorStream();
}

///////////////////////////////////////////////////////////////
CategoryStream Logger::LogInfo() const
{
    return m_category.infoStream();
}

///////////////////////////////////////////////////////////////
CategoryStream Logger::LogWarn() const
{
    return m_category.warnStream();
}

///////////////////////////////////////////////////////////////
log4cpp::Appender * Logger::GetAppender( const std::string & a_name, LoggerType a_type )
{
    switch( a_type )
    {
    case FILE_LOGGER: return new log4cpp::FileAppender( a_name, a_name + ".log" );
    default: return new log4cpp::OstreamAppender( a_name, &std::cout );
    }
}
